/**   
 * Copyright © 2018 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Package: com.wxsoft.java.classload.demo 
 * @author: chenliang   
 * @date: 2018年6月22日 下午8:51:30 
 */
package com.wxsoft.java.classload.demo;


/**
 * @author chenliang
 *
 */
public class ClassLoaderTest3 {
	public static void main(String[] args) throws ClassNotFoundException {
//		String rootDir = "/Users/zejian/Downloads/Java8_Action/src/main/java/";
		String rootDir = "E:\\workspace_eclipse_luna\\demo\\target\\classes\\";
		// 创建自定义文件类加载器
		FileClassLoader loader = new FileClassLoader(rootDir);
		FileClassLoader loader2 = new FileClassLoader(rootDir);

		try {
			// 加载指定的class文件,调用loadClass()
			Class<?> object1 = loader
					.loadClass("com.wxsoft.java.classload.demo.DemoObj");
			DemoObj dObj1 = new DemoObj();
			System.out.println(dObj1.toString());
			System.out.println(dObj1.toString());
			Class<?> object2 = loader2
					.loadClass("com.wxsoft.java.classload.demo.DemoObj");
			DemoObj dObj2 = (DemoObj) object2.newInstance();
			System.out.println(dObj2.toString());
//			Method method = object2.getMethod("toString");
//			System.out.println(method.invoke(dObj2));

			System.out.println("loadClass->obj1:" + object1.hashCode());
			System.out.println("loadClass->obj2:" + object2.hashCode());

			// 加载指定的class文件,直接调用findClass(),绕过检测机制，创建不同class对象。
			Class<?> object3 = loader
					.findClass("com.wxsoft.java.classload.demo.DemoObj");
			DemoObj dObj3 = new DemoObj();
			System.out.println(dObj3.toString());
			Class<?> object4 = loader2
					.findClass("com.wxsoft.java.classload.demo.DemoObj");
			DemoObj dObj4 = new DemoObj();
			System.out.println(dObj4.toString());

			System.out.println("loadClass->obj3:" + object3.hashCode());
			System.out.println("loadClass->obj4:" + object4.hashCode());

			/**
			 * 输出结果: 
			 * loadClass->obj1:644117698 
			 * loadClass->obj2:644117698
			 * findClass->obj3:723074861 
			 * findClass->obj4:895328852
			 */

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
